package com.vshop.service.agatewaytobeijing;

import com.alibaba.fastjson.JSON;
import com.vshop.config.dto.ResultDataDto;
import com.vshop.config.dto.StringOutputConstant;
import com.vshop.entity.agatewaytobeijing.JsonConstant;
import com.vshop.entity.agatewaytobeijing.querydevice.iot.request.RequestQueryDevices;
import com.vshop.entity.agatewaytobeijing.querydevice.iot.response.ResponseQueryDevices;
import com.vshop.entity.agatewaytobeijing.querypartner.request.RequestQueryPartner;
import com.vshop.entity.agatewaytobeijing.querypartner.response.ResponseQueryPartner;
import com.vshop.entity.alast.common.ServicePlanOrderAutoRenewalRecord;
import com.vshop.entity.alast.single.autorenewal.cancel.request.RequestStripeSubCancel;
import com.vshop.entity.requestinterface.querypackage.request.RequestPackageQueryNew;
import com.vshop.entity.requestinterface.querypackage.response.ResponsePackageQueryNew;
import com.vshop.utils.HttpUtil;
import com.vshop.utils.Md5Encrypt;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 所有需要去请求北京企业网关的业务实现类：
 *
 * @author lm
 * @date 2020/11/6 11:48
 */
@Component
@Slf4j
public class GatewayToIotService {
    /**
     * 请求深圳iot项目的地址
     */
    @Value("${iot.requestURI}")
    private String iotRequestURI;
    /**
     * 请求深圳pay项目的地址
     */
    @Value("${pay.requestURI}")
    private String payRequestURI;

    /**
     * 请求深圳iot网关 3.3 查询单一设备实时信息
     *
     * @param deviceCode
     * @param partnerCode
     * @param secret
     * @return
     */
    public ResponseQueryDevices queryDevices(String deviceCode, String partnerCode, String secret) {
        if (StringUtils.isBlank(iotRequestURI)) {
            return null;
        }
        RequestQueryDevices re = new RequestQueryDevices();
        re.setDeviceId(deviceCode);
        re.setPartnerCode(partnerCode);
        re.setSecret(secret);

        String request = JSON.toJSONString(re);
        log.info("请求【查询设备】：" + request);
        String url = iotRequestURI + JsonConstant.IOT_JSON_QUERY_DEVICE;
        ;
        // 发送post请求，并接收返回结果
        String response = HttpUtil.httpPost(url, request);
        log.info("url=" + url);

        log.info("响应【查询设备】：" + response);

        if (StringUtils.isBlank(response)) {
            return null;
        }
        return JSON.parseObject(response, ResponseQueryDevices.class);
    }

    /***
     * 请求深圳iot网关 3.25 根据设备标识查询所属企业 For Shop
     *
     * @param deviceCode
     * @return
     */
    public ResponseQueryPartner queryPartnerCode(String deviceCode) {
        if (StringUtils.isBlank(iotRequestURI)) {
            return null;
        }
        RequestQueryPartner query = new RequestQueryPartner();
        query.setDeviceCode(deviceCode);

        //组装需要的json去请求报文
        String request = JSON.toJSONString(query);
        log.info("请求【iccId查询企业】：" + request);
        String url = iotRequestURI + JsonConstant.IOT_JSON_QUERY_PARTNER_CODE;

        // 发送post请求，并接收返回结果
        String response = HttpUtil.httpPost(url, request);
        log.info("url=" + url);

        log.info("响应【iccId查询企业】：" + response);
        if (StringUtils.isBlank(response)) {
            return null;
        }
        return JSON.parseObject(response, ResponseQueryPartner.class);
    }

    /****
     * 2.0接口：：请求深圳iot网关 3.27 H5 查询套餐 (有首单信息的For Shop)
     *
     * @param deviceCode
     * @param orderId
     * @param orderStatus
     * @param orderType
     * @param partnerCode
     * @param secret
     * @return
     */
    public ResponsePackageQueryNew queryH5Order(String deviceCode, String orderId, String orderStatus, String orderType, String partnerCode, String secret) {
        if (StringUtils.isBlank(iotRequestURI)) {
            return null;
        }
        RequestPackageQueryNew query = new RequestPackageQueryNew();
        query.setDeviceId(deviceCode);
        query.setOrderId(orderId);
        query.setOrderStatus(orderStatus);
        query.setOrderType(orderType);
        query.setPartnerCode(partnerCode);
        query.setSecret(secret);

        if (StringOutputConstant.CODE_TWO.equals(orderStatus)) {
            query.setOrderStatus(orderStatus);
            //查询有没有正在使用的订单
            String request = JSON.toJSONString(query);
            log.info("请求查询【正在使用】订单：" + request);
            String url = iotRequestURI + JsonConstant.IOT_JSON_QUERY_H5_ORDER;

            // 发送post请求，并接收返回结果
            String response = HttpUtil.httpPost(url, request);
            log.info("url=" + url);

            log.info("响应查询【正在使用】订单：" + response);
            if (StringUtils.isBlank(response)) {
                return null;
            }

            return JSON.parseObject(response, ResponsePackageQueryNew.class);

        } else if (StringOutputConstant.CODE_ONE.equals(orderStatus)) {
            query.setOrderStatus(orderStatus);
            //查询有没有正在使用的订单
            String request = JSON.toJSONString(query);
            log.info("请求查询【未启用】订单：" + request);
            String url = iotRequestURI + JsonConstant.IOT_JSON_QUERY_H5_ORDER;

            // 发送post请求，并接收返回结果
            String response = HttpUtil.httpPost(url, request);
            log.info("url=" + url);

            log.info("响应查询【未启用】订单：" + response);
            if (StringUtils.isBlank(response)) {
                return null;
            }

            return JSON.parseObject(response, ResponsePackageQueryNew.class);

        } else {
            query.setOrderStatus(orderStatus);
            //查询有没有正在使用的订单
            String request = JSON.toJSONString(query);
            log.info("请求查询【所有】订单：" + request);
            String url = iotRequestURI + JsonConstant.IOT_JSON_QUERY_H5_ORDER;

            // 发送post请求，并接收返回结果
            String response = HttpUtil.httpPost(url, request);
            log.info("url=" + url);

            log.info("响应查询【所有】订单：" + response);
            if (StringUtils.isBlank(response)) {
                return null;
            }
            return JSON.parseObject(response, ResponsePackageQueryNew.class);
        }
    }

    /**
     * 获取stripe收银台自动续费取消订阅
     *
     * @param record
     * @return
     */
    public ResultDataDto stripeSubCancel(ServicePlanOrderAutoRenewalRecord record) {
        if (StringUtils.isBlank(payRequestURI)) {
            return null;
        }
        RequestStripeSubCancel cancel = new RequestStripeSubCancel();
        cancel.setTradeType("stripeSubCancel");
        cancel.setTradeTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        cancel.setVersion("2.0");
        cancel.setPartnerCode(record.getPartnerCode());
        cancel.setEmail(record.getEmail());
        cancel.setPayId(record.getPayId());
        cancel.setPlan_id(record.getPlanId());
        cancel.setPm_id(record.getPmId());
        cancel.setSub_id(record.getSubId());
        cancel.setSign(Md5Encrypt.jsonSign(cancel));
        //组装需要的json去请求报文
        String request = JSON.toJSONString(cancel);
        log.info("请求【自动续费取消订阅】：" + request);
        String url = payRequestURI + JsonConstant.PAY_STRIPE_SUB_CANCEL;

        // 发送post请求，并接收返回结果
        String response = HttpUtil.httpPost(url, request);
        log.info("url=" + url);

        log.info("响应【自动续费取消订阅】：" + response);
        if (StringUtils.isBlank(response)) {
            return null;
        }
        return JSON.parseObject(response, ResultDataDto.class);
    }
}
